<?php
/**
 * @see Alinks_ImportExport_Export_Interface
 */
require_once('Alinks/ImportExport/Export/Interface.php');

/**
 * @see Alinks_Keyphrase
 *
 */
require_once('Alinks/Keyphrase.php');

/**
 * Abstract class for export classes
 *
 * Copyright (C) 2008  Sean Hickey
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Sean Hickey <sean@headzoo.com>
 * @version $Id: Abstract.php 390 2008-07-03 05:40:51Z sean $
 * @package Alinks_ImportExport
 */
abstract class Alinks_ImportExport_Export_Abstract implements Alinks_ImportExport_Export_Interface
{
	/**
	 * Whether to export settings
	 */
	const EXPORT_SETTINGS = 'settings';
	
	/**
	 * Whehter to export factory settings
	 */
	const EXPORT_FACTORY_SETTINGS = 'factory_settings';
	
	/**
	 * Export only keyphrases in the array of hashes
	 */
	const EXPORT_HASHES = 'hashes';
	
	/**
	 * Array of export options
	 *
	 * @var array
	 */
	protected $_options = array();
	
	/**
	 * Constructor
	 *
	 * @param array $options Export options
	 */
	public function __construct(array $options = array())
	{
		$this->setOptions($options);
	}
	
	/**
	 * Sets the export options
	 *
	 * @param array $options The export options
	 * @return Alinks_ImportExport_Export_Abstract
	 */
	public function setOptions(array $options)
	{
		$this->_resetOptions();
		foreach($options as $key => $value) {
			$this->addOption($key, $value);
		}
		
		return $this;
	}
	
	/**
	 * Adds a single export option
	 *
	 * @param string $key The option key
	 * @param mixed $value The option value
	 * @return Alinks_ImportExport_Export_Abstract
	 */
	public function addOption($key, $value)
	{
		$this->_options[$key] = $value;
		return $this;
	}
	
	/**
	 * Returns the export options
	 *
	 * If $key is specified, then only the option value for that
	 * key will be returned. Otherwise the full array of options
	 * will be returned.
	 *
	 * @param string $key The option key
	 * @return mixed
	 * @throws Alinks_ImportExport_Export_Exception
	 */
	public function getOptions($key = null)
	{
		if (null !== $key && !isset($this->_options[$key])) {
			/**
			 * @see Alinks_ImportExport_Export_Exception
			 */
			require_once('Alinks/ImportExport/Export/Exception.php');
			throw new Alinks_ImportExport_Export_Exception("The option value for $key key is not set");
		}
		
		if (null !== $key) {
			return $this->_options[$key];
		}
		
		return $this->_options;
	}
	
	/**
	 * Sets the options to their default values
	 */
	protected function _resetOptions()
	{
		$this->_options = array();
		$this->_options[self::EXPORT_SETTINGS] = false;
		$this->_options[self::EXPORT_FACTORY_SETTINGS] = false;
		$this->_options[self::EXPORT_HASHES] = array();
	}
}